﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Globalization;
using System.IO;

namespace InnovTech.Util.Resources.Test
{
    [TestClass]
    [DeploymentItem("TestStuffs")]
    public class TestResxFile
    {
        static string AppFrameNeutralResx = "AppFrameUIResources.resx";
        static string AppFrameUSResx = "AppFrameUIResources.en-US.resx";

        static string ControlCenterNeutralResx = "AppFrameControlCenterUIResources.resx";
        static string ControlCenterUSResx = "AppFrameControlCenterUIResources.en-US.resx";

        [TestMethod]
        public void TestIsResxFile()
        {
            Assert.IsTrue(ResxFile.IsResxFile("AppFrameUIResources.resx"));
            Assert.IsFalse(ResxFile.IsResxFile("AppFrameUIResources.res"));
        }
        [TestMethod]
        public void TestIsNeutralResxFile()
        {
            Assert.IsTrue(ResxFile.IsNeutralResxFileName("AppFrameUIResources.resx"));
            Assert.IsTrue(ResxFile.IsNeutralResxFile(new ResxFile("", "AppFrameUIResources.resx")));
            Assert.IsFalse(ResxFile.IsNeutralResxFileName("AppFrameUIResources.en-US.resx"));
        }
        [TestMethod]
        public void TestGetCulture()
        {
            Assert.AreEqual(null, ResxFile.GetCulture(AppFrameNeutralResx));
            Assert.AreEqual(new CultureInfo("en-US"), ResxFile.GetCulture(AppFrameUSResx));
            Assert.AreEqual(null, ResxFile.GetCulture(ControlCenterNeutralResx));
            Assert.AreEqual(new CultureInfo("en-US"), ResxFile.GetCulture(ControlCenterUSResx));
        }

        [TestMethod]
        public void TestGetNeutralResxFileName()
        {
            Assert.AreEqual(Path.GetFileNameWithoutExtension(AppFrameNeutralResx), 
                ResxFile.GetNeutralResxFileName(AppFrameNeutralResx));

            Assert.AreEqual(Path.GetFileNameWithoutExtension(AppFrameNeutralResx),
                ResxFile.GetNeutralResxFileName(AppFrameUSResx));

            Assert.AreEqual(Path.GetFileNameWithoutExtension(ControlCenterNeutralResx),
                ResxFile.GetNeutralResxFileName(ControlCenterNeutralResx));

            Assert.AreEqual(Path.GetFileNameWithoutExtension(ControlCenterNeutralResx),
                ResxFile.GetNeutralResxFileName(ControlCenterUSResx));
        }

        [TestMethod]
        public void TestConstructor_Successfully_Creation()
        {
            string alias = "abc";

            //non-neutral
            ResxFile file = new ResxFile(alias, AppFrameUSResx);

            Assert.AreEqual(alias, file.Alias);
            Assert.AreEqual(AppFrameUSResx, file.FilePath);
            Assert.AreEqual(new CultureInfo("en-US"), file.Language);
            Assert.IsFalse(file.IsNeutral);
            Assert.AreEqual("AppFrameUIResources", file.RelatedNeutralFileName);


            //neutral
            file = new ResxFile(alias, AppFrameNeutralResx);

            Assert.AreEqual(alias, file.Alias);
            Assert.AreEqual(AppFrameNeutralResx, file.FilePath);
            Assert.AreEqual(null, file.Language);
            Assert.IsTrue(file.IsNeutral);
            Assert.AreEqual("AppFrameUIResources", file.RelatedNeutralFileName);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestConstructor_FailedBy_NullFileName()
        {
            ResxFile file = new ResxFile("", null);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void TestConstructor_FailedBy_NotResxFileName()
        {
            ResxFile file = new ResxFile("", "dsjkfss.rexs");
        }
        [TestMethod]
        public void TestLoad()
        {
            ResxFile file = new ResxFile("", Directory.GetCurrentDirectory() + "\\" + AppFrameNeutralResx);
            file.Load();
            Assert.AreEqual(20, file.Count);

            file.SetValue("button_ok", "ooo", "changed");
            file.Save();
        }

        [TestMethod]
        public void TestSave()
        {
            ResxFile file = new ResxFile("", Directory.GetCurrentDirectory() + "\\" + AppFrameNeutralResx);
            file.Load();
            Assert.AreEqual(20, file.Count);

            file.SetValue("button_ok", "ooo", "changed");
            Assert.AreEqual("ooo", file.GetValue("button_ok"));
            Assert.AreEqual("changed", file.GetComment("button_ok"));
            file.Save(Directory.GetCurrentDirectory() + "\\test.resx");

            file = new ResxFile("", Directory.GetCurrentDirectory() + "\\test.resx");
            file.Load();
            Assert.AreEqual(20, file.Count);
            Assert.AreEqual("ooo", file.GetValue("button_ok"));
            Assert.AreEqual("changed", file.GetComment("button_ok"));
        }
        [TestMethod]
        public void TestGetSetValue()
        {
            ResxFile file = new ResxFile("", Directory.GetCurrentDirectory() + "\\" + AppFrameNeutralResx);
            Assert.IsFalse(file.ContainsKey("button_ok"));
            file.SetValue("button_ok", "ooo", "changed");
            Assert.IsTrue(file.ContainsKey("button_ok"));
            Assert.AreEqual("ooo", file.GetValue("button_ok"));
            Assert.AreEqual("changed", file.GetComment("button_ok"));
        }
    }
}
